const { think } = require('thinkjs');
const Base = require('./base.js');
const moment = require('moment/moment.js');
const { default: BigNumber } = require('bignumber.js');

module.exports = class extends Base {
  async normalPanelAction() {
    const contract_daoqi = await this.model('contract_list')
      .where({
        type: 1,
        date_end: [
          'between',
          moment().format('YYYY-MM-DD'),
          moment()
            .add(6, 'months')
            .format('YYYY-MM-DD'),
        ],
      })
      .count();
    const contract_guakao = await this.model('contract_list')
      .where({
        type: 3,
        contract_type: 70,
      })
      .count();

    const contract_guoqi = await this.model('contract_list')
      .where({
        status: 2,
        type: 1,
      })
      .count();

    const contract_end = await this.model('contract_list')
      .where({
        status: 3,
        type: 1,
      })
      .count();

    const contract_ing = await this.model('contract_list')
      .where({
        status: 1,
        type: 1,
      })
      .count();

    const zengzhi_ing = await this.model('contract_list')
      .where({
        status: 1,
        type: 3,
      })
      .count();

    const zengzhi_end = await this.model('contract_list')
      .where({
        status: 2,
        type: 3,
      })
      .count();

    const unInTotal =
      (await this.model('income_list')
        .where({
          status: 0,
          delay: 1,
          pass: 0,
          type: ['!=', 'yajin'],
        })
        .sum('money')) || 0;

    const unOutTotal =
      (await this.model('output_list')
        .where({ status: 0, delay: 1, type: ['!=', 'yajin'] })
        .sum('money')) || 0;

    return this.success({
      unOutTotal,
      unInTotal,
      contract_daoqi: contract_daoqi,
      contract_guakao,
      contract_guoqi,
      contract_end,
      contract_ing,
      zengzhi_ing,
      zengzhi_end,
    });
  }

  async contractCycleAction() {
    const dateFilter = this.post('t');

    const start = moment(dateFilter[0]).format('YYYY-MM-DD');
    const end = moment(dateFilter[1]).format('YYYY-MM-DD');

    const list = await this.model('contract_list')
      .where({ type: 1, date_start: ['between', [start, end]] })
      .field('fangzu, wuye, date_start, date_end')
      .select();
    const arr = [];
    for (const item of list) {
      const cycle = moment(item.date_end).diff(
        moment(item.date_start),
        'months'
      );
      const money = BigNumber(item.fangzu)
        .plus(BigNumber(item.wuye))
        .toFixed(2);
      arr.push([cycle, money]);
    }
    return this.success(arr);
  }

  async contractCatgoryAction() {
    const dateFilter = this.post('t');

    const start = moment(dateFilter[0]).format('YYYY-MM-DD');
    const end = moment(dateFilter[1]).format('YYYY-MM-DD');

    const contract_sum_fangzu =
      (await this.model('contract_list')
        .where({ type: 1, date_start: ['between', [start, end]] })
        .sum('fangzu')) || 0;
    const contract_sum_wuye =
      (await this.model('contract_list')
        .where({ type: 1, date_start: ['between', [start, end]] })
        .sum('wuye')) || 0;
    const contract_sum_zengzhi =
      (await this.model('contract_list')
        .where({ type: 3, date_start: ['between', [start, end]] })
        .sum('contract_total')) || 0;

    const contract_sum = BigNumber(contract_sum_fangzu)
      .plus(BigNumber(contract_sum_wuye))
      .toFixed(2);
    return this.success([
      { name: '租赁合同', value: contract_sum },
      { name: '增值合同', value: contract_sum_zengzhi },
    ]);
  }

  async normalGuakaoAction() {
    const dateFilter = this.post('t');

    const start = moment(dateFilter[0]).format('YYYY-MM-DD');
    const end = moment(dateFilter[1]).format('YYYY-MM-DD');

    const project = await this.model('project_list')
      .where({ type: 1 })
      .field('name, id')
      .select();
    const arr = [];
    for (const item of project) {
      const count = await this.model('contract_list')
        .where({
          project_id: item.id,
          type: 3,
          contract_type: 70,
          // date_start: ['between', [start, end]],
        })
        .count();

      arr.push({
        name: item.name,
        value: count,
      });
    }
    return this.success(arr);
  }

  async contractLineAction() {
    const dateFilter = this.post('t');

    const start = moment();
    const startData = moment();
    const end = moment().add(3, 'years');

    const project = await this.model('project_list')
      .where({ type: 1 })
      .field('name, id')
      .select();

    // const days = [end];

    const num = end.diff(start, 'months');

    const data = {
      data: [],
    };

    const xData = [];

    for (let i = 0; i < num; i++) {
      xData.push(start.add(1, 'months').format('YYYY-MM'));
    }

    data.xData = xData;

    for (let i = 0; i < project.length; i++) {
      const addData = {
        name: project[i].name,
        type: 'line',
        showSymbol: false,
        stack: 'Total',
        data: [],
      };

      for (let j = 0; j < num; j++) {
        let total =
          (await this.model('project_list')
            .where({
              project_id: project[i].id,
              type: 4,
              date_end: [
                'between',
                moment('2000-01-01').format('YYYY-MM-DD HH:MM'),
                startData
                  .add(1, 'months')
                  .endOf('month')
                  .format('YYYY-MM-DD HH:MM'),
              ],
            })
            .sum('rest')) || 0;
        total = BigNumber(0)
          .minus(BigNumber(total))
          .toFixed();
        addData.data.push(total);
      }
      data.data.push(addData);
    }
    return this.success(data);
  }
};
